Linux basics cheat-sheet
1. Basic Commands:
pwd: Prints the current working directory.ls: Lists the files and directories in the current directory.ls -l: Lists files in long format (with permissions, owner, etc.).ls -a: Lists all files, including hidden ones.cd [directory]: Changes the directory.cd ..: Goes back to the previous directory (parent directory).cd ~: Changes to the home directory.cd -: return to Previous PATH/directorymkdir [directory_name]: Creates a new directory.rmdir [directory_name]: Removes an empty directory.touch [file_name]: Creates an empty file or updates the modification time if the file exists.cp [source][destination]: Copies a file or directory.mv [source] [destination]: Moves or renames a file or directory.rm [file_name]: Removes a file.rm -r [directory_name]: Removes a directory and its contents recursively.cat [file_name]: Displays the contents of a file.nano [file_name]: Opens a file in the nano text editor.clear: Clears the terminal screen.
2. System Information:
uname -a: Displays information about the system.df -h: Shows disk space usage in human-readable format.du -sh [directory]: Displays the disk usage of a directory.top: Shows a real-time view of system processes and resource usage.htop: An improved version of top (requires installation: sudo apt install htop).free -h: Displays the amount of free and used memory in the system.uptime: Shows how long the system has been running.whoami: Prints the current logged-in username.hostname: Displays the system's hostname.
3. File Permissions and Ownership:
chmod [permissions] [file]: Changes the file's permissions.- Example:
chmod 755 file.shgives the owner full permissions, and others read and execute permissions. - chmod +x [file]: Makes a file executable.
- Example:
chown [user:group] [file]: Changes the file's ownership.- Example:
chown user:user file.txt
- Example:
4. Network:
- ifconfig: Displays network interface configuration.
- ip a: Shows IP address and network interfaces.
- ping [domain]: Tests network connectivity to a domain or IP address.
- curl [URL]: Downloads content from a URL.
- wget [URL]: Another way to download files from a URL.
- ssh [user]@[hostname]: Connects to a remote system via SSH.
5. Package Management (APT):
sudo apt update: Updates the package list.sudo apt upgrade: Installs available updates.sudo apt install [package_name]: Installs a packagesudo apt remove [package_name]: Removes a package.sudo apt autoremove: Removes unnecessary packages.
6. Process Management:
ps aux: Lists running processes.kill [PID]: Kills a process by its PID (Process ID).killall [process_name]: Kills all instances of a specific process.bg: Resumes a suspended job in the background.fg: Brings a background job to the foreground.
7. User Management:
sudo adduser [username]: Adds a new user.sudo deluser [username]: Deletes a user.passwd [username]: Changes the password for a user.
8. Archiving and Compression:
tar -cvf archive.tar [files]: Creates a tar archive.tar -xvf archive.tar: Extracts a tar archive.gzip [file]: Compresses a file using gzip.gunzip [file.gz]: Decompresses a gzip file.
9. Searching:
find [path] -name [file_name]: Finds a file by name.grep '[pattern]' [file]: Searches for a pattern in a file.- Example:
grep 'error' log.txtwill search for the word 'error' in the log.txt file.
- Example:
10. Permissions (Special Commands):
sudo [command]: Runs a command as the superuser (admin privileges).su [username]: Switches to another user.
11. Disk Management:
fdisk -l: Lists all available partitions.mount [device] [directory]: Mounts a device to a directory.umount [device/directory]: Unmounts a device.
12. History and Shortcuts:
- history: Displays the list of recently executed commands.
- !!: Repeats the last command.
- !n: Executes the nth command in history.
Ctrl + C: Stops the current command.Ctrl + Z: Suspends the current command.Ctrl + A: Moves the cursor to the beginning of the line.Ctrl + E: Moves the cursor to the end of the line.
13. Other Commands:
• wget -c [URL]: Downloads a file from a URL and continues if interrupted.
• curl -O [URL]: Downloads a file from a URL.
14. File Compression and Decompression:
zip [archive.zip] [file1] [file2]: Creates a zip archive.unzip [archive.zip]: Extracts a zip archive.tar -cvzf archive.tar.gz [files]: Creates agzippedtar archive.tar -xvzf archive.tar.gz: Extracts a gzip
15. The Help (man && --help):
manstands for manual it helps you to viewmanualhow to use thecommands.mancommand in Linux its useful in case no network connection*.- It is ideal for deep understanding or troubleshooting more complex command behavior. also It requires that manual pages are installed on the system.
- The
--helpflag provides a quick, concise overview of a command's usage, available options, and syntax.- It is useful for fast reference when you need a basic understanding or want to quickly see
available flags. Not allcommands support --help.
- It is useful for fast reference when you need a basic understanding or want to quickly see
16. Difference Between Switch and Flag
- Switch (
-): Short, single-letter option.
Example:ls -a→ show hidden files.
- Flag (
--): Long, descriptive option.
Example:ls --help→ show help message.